package com.rogers.rmcdouga.skirmishgadget.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class AddTokenDialog extends DialogBox implements ImageURLBox.URLBox_ValidityCallback
{

	private static final String ADD_TOKEN = "Add Token";

	private static addTokenDialogUiBinder uiBinder = GWT.create(addTokenDialogUiBinder.class);

	interface addTokenDialogUiBinder extends UiBinder<Widget, AddTokenDialog> {	}
	
	// State variables
	private boolean customTokenSize_state = false;	// Has the user chosen to make the token size custom?


	@UiField
	ImageURLBox tokenURLBox;

	@UiField
	Button okButton;

	@UiField
	Button cancelButton;

	@UiField
	RadioButton tokenSize_5x5;

	@UiField
	RadioButton tokenSize_10x10;
	
	@UiField
	RadioButton tokenSize_CustomSize;

	@UiField
	TextBox tokenWidth;

	@UiField
	TextBox tokenHeight;

	private void init()
	{
		setText(ADD_TOKEN);
		setWidget(uiBinder.createAndBindUi(this));
		tokenURLBox.setCallback(this);
	}

	public AddTokenDialog() {
		super();
		init();
	}

	public AddTokenDialog(boolean autoHide, boolean modal) {
		super(autoHide, modal);
		init();
	}

	public AddTokenDialog(boolean autoHide) {
		super(autoHide);
		init();
	}

	@UiHandler("okButton")
	void okButtonClick(ClickEvent e)
	{
		hide();
	}
	
	@UiHandler("cancelButton")
	void cancelButtonClick(ClickEvent e)
	{
		hide();
	}

	@UiHandler("tokenSize_5x5")
	void MapSize_TokenSize_5x5_Click(ClickEvent e)
	{
		setCustomTokenSize_state(false);
	}

	@UiHandler("tokenSize_10x10")
	void MapSize_TokenSize_10x10_Click(ClickEvent e)
	{
		setCustomTokenSize_state(false);
	}

	@UiHandler("tokenSize_CustomSize")
	void MapSize_tokenSize_CustomSize_Click(ClickEvent e)
	{
		setCustomTokenSize_state(true);
	}

	private boolean isCustomTokenSize_state()
	{
		return customTokenSize_state;
	}

	private void setCustomTokenSize_state(boolean customTokenSizeState)
	{
		customTokenSize_state = customTokenSizeState;
		enableAndDisableFields();
	}

	@Override
	public void invalidURL()
	{
		enableAndDisableFields();
	}

	@Override
	public void validURL()
	{
		enableAndDisableFields();
	}
	
	private void enableAndDisableFields()
	{
		if (!tokenURLBox.isValidURL())
		{
			okButton.setEnabled(false);
			tokenSize_5x5.setEnabled(false);
			tokenSize_10x10.setEnabled(false);
			tokenSize_CustomSize.setEnabled(false);
			tokenWidth.setEnabled(false);
			tokenHeight.setEnabled(false);
		}
		else
		{
			okButton.setEnabled(true);
			tokenSize_5x5.setEnabled(true);
			tokenSize_10x10.setEnabled(true);
			tokenSize_CustomSize.setEnabled(true);
			if (isCustomTokenSize_state())
			{
				tokenWidth.setEnabled(true);
				tokenHeight.setEnabled(true);
			}
			else
			{
				tokenWidth.setEnabled(false);
				tokenHeight.setEnabled(false);
			}
		}
	}
}
